package com.example.demo.sxt;

import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.List;

/**
 * @author : zhy
 * @since : 2022/7/19
 */
public class HeadObj extends GameObj{

    //定义变量，控制方向
    private String direction="right";

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    public HeadObj(Image img, int x, int y, GameWin gameWin) {
        super(img, x, y, gameWin);
        //添加键盘监听事件
        this.gameWin.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                changeDirection(e);
            }
        });
    }

    //控制移动方向
    public void changeDirection(KeyEvent event)
    {
        switch (event.getKeyCode())
        {
            case KeyEvent.VK_A:
                if(!"right".equals(direction)){
                    direction="left";
                    img=GameUtil.leftImg;

                }
                break;
            case KeyEvent.VK_D:
                if(!"left".equals(direction)){
                    direction="right";
                    img=GameUtil.rightImg;

                }
                break;
            case KeyEvent.VK_W:
                if(!"down".equals(direction)){
                    direction="up";
                    img=GameUtil.upImg;

                }
                break;
            case KeyEvent.VK_S:
                if(!"up".equals(direction)){
                    direction="down";
                    img=GameUtil.downImg;

                }
                break;
            default:break;
        }
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);
        //蛇吃食物食物
        FoodObj foodObj=this.gameWin.foodObj;
        //蛇身最后一节的坐标
        Integer newX=null;
        Integer newY=null;
        if(this.x==foodObj.x&&this.y==foodObj.y){

            this.gameWin.foodObj=foodObj.getFood();
            //获取蛇身最后一个元素
            BodyObj lastBody=this.gameWin.bodyObjList.get(this.gameWin.bodyObjList.size()-1);
            newX=lastBody.x;
            newY=lastBody.y;
            //蛇吃食物分数加1
            this.gameWin.score+=1;
        }
        //通关判断
        if(GameUtil.level==1){
            if(this.gameWin.score>=15){
                //通关
                GameWin.state=4;
            }
        }else if(GameUtil.level==2){
            if(this.gameWin.score>=20){
                //通关
                GameWin.state=4;
            }
        }else if(GameUtil.level==3){
            if(this.gameWin.score>=30){
                //通关
                GameWin.state=4;
            }
        }

        move();
        //添加蛇的身体
        if(newX!=null&&newY!=null)
        {
            this.gameWin.bodyObjList.add(new BodyObj(GameUtil.bodyImg,newX,newY,this.gameWin));
        }
        //越界处理
        if(x<0) {
            x=570;
        }else if(x>570){
            x=0;
        }else if(y<30){
            y=570;
        }else if(y>570)
        {
            y=30;
        }

    }

    //蛇的移动
    public void move()
    {
        //蛇身移动
        List<BodyObj> bodyObjList=this.gameWin.bodyObjList;
        for(int i=bodyObjList.size()-1;i>=1;i--){
            bodyObjList.get(i).x=bodyObjList.get(i-1).x;
            bodyObjList.get(i).y=bodyObjList.get(i-1).y;

            //蛇头与蛇身的判断
            if(this.x==bodyObjList.get(i).x&&this.y==bodyObjList.get(i).y)
            {
                //游戏失败
                GameWin.state=3;
            }
        }
        bodyObjList.get(0).x=this.x;
        bodyObjList.get(0).y=this.y;
        //蛇头移动
        switch (direction){
            case "up":
                y-=height;
                break;
            case "down":
                y+=height;
                break;
            case "left":
                x-=width;
                break;
            case "right":
                x+=width;
                break;
        }
    }
}
